package com.ifast.integral.service.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.ifast.integral.dao.IntegralHistoryDao;
import com.ifast.integral.domain.IntegralHistory;
import com.ifast.integral.service.IntegralHistoryService;
import com.luculent.util.extension.api.PageResult;
import com.xiaoleilu.hutool.util.CollectionUtil;

import cn.hutool.core.bean.BeanUtil;


/**
 * 
 * @author ShiQiang
 * @date 2019年4月9日09:56:53
 */
@Transactional
@Service("integralHistoryService")
public class IntegralHistoryServiceImpl extends ServiceImpl<IntegralHistoryDao, IntegralHistory> implements IntegralHistoryService{
	/**
	 * 试题分页查询
	 */
	@Override
	public PageResult<?> pageList(String userId,String dwCode,String nian,String yue,String ri, Page<IntegralHistory> page) {
		//返回数据进行拼装 
	    return (StringUtils.isBlank(userId) || StringUtils.isBlank(dwCode))? PageResult.fail_msg("缺少必要参数") : 
	    	data2Page(userId,this.selectPage(page, new EntityWrapper<IntegralHistory>()
	    		.eq("DW_CODE",dwCode) 
	    		.eq("USER_ID",userId)
		    	.eq(StringUtils.isNotBlank(nian),"NIAN",nian)
		    	.eq(StringUtils.isNotBlank(yue),"YUE",yue)
		    	.eq(StringUtils.isNotBlank(ri),"RI",ri)
		    	.orderBy("NIAN",false).orderBy("YUE",false).orderBy("RI",false).orderBy("CREATETIME",false)));
	}
	/**
	 * 将查询的试卷信息 进行拼装
	 * */
	private  PageResult<?> data2Page(String userId,Page<IntegralHistory> pageAll){
		PageResult<Map<String,Object>> pageRes = new PageResult<>(); 
		if(CollectionUtil.isEmpty(pageAll.getRecords())){ 
			pageRes.setPageNum(1);
			pageRes.setHasNext(false);
	        return pageRes;
	    } 
		List<Map<String,Object>> map = Lists.newArrayList();
		pageAll.getRecords().stream().forEach(version ->{ 
			map.add(BeanUtil.beanToMap(version));
			
		});
		pageRes.setListData(map);
		pageRes.setPageNum(pageAll.getCurrent());
		pageRes.setHasNext(pageAll.hasNext());
		return  pageRes;
	}
}